package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RoleServiceImpl
 * @Description
 * @Author Mr.Ho
 * @Date 9:42 2019/7/17
 * @Version 1.1
 **/

@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;


    //分页查询
    @Override
    public PageResult<Role> findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        //模糊查询需要凭借
        if(!StringUtils.isEmpty(queryPageBean.getQueryString()))
        {
            queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
        }
        //使用分页插件
        PageHelper.startPage(currentPage,queryPageBean.getPageSize());
        //紧接着查询的语句会被拦截进行分页  查询所有
        Page<Role> page = roleDao.findByCondition(queryPageBean.getQueryString());
        //使用pageresult包装
        PageResult<Role> result = new PageResult<>(page.getTotal(), page.getResult());
        return result;
    }


    //添加
    @Override
    @Transactional
    public void add(Integer[] permissionIds,Integer[] menuIds, Role role) {
        roleDao.add(role);
        setRoleAndPermission(role.getId(),permissionIds);
        setRoleAndMenu(role.getId(),menuIds);

    }


    public void setRoleAndPermission(Integer RoleId, Integer[] permissionIds) {
        if(permissionIds!=null&&permissionIds.length>0)
        {
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",RoleId);
                map.put("permission_id",permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

    public void setRoleAndMenu(Integer RoleId, Integer[] menuIds) {
        if(menuIds!=null&&menuIds.length>0)
        {
            for (Integer menuId : menuIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",RoleId);
                map.put("menu_id",menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }


    @Override
    @Transactional
    public void deleteById(int id) {
        int count1 = roleDao.countPermissionIdsByRoleId(id);
        int count2 = roleDao.countMenuIdsByRoleId(id);
        int count3 = roleDao.countUserIdsByRoleId(id);
        int count = count1+count2+count3;
        if(count>0)
        {
            throw new RuntimeException(MessageConstant.DELETE_ROLE_USED);
        }
        roleDao.deleteById(id);
    }

    @Override
    @Transactional
    public void edit(Integer[] permissionIds, Integer[] menuIds, Role role) {
        //根据role_id 删除中间表原有的数据
        roleDao.deleteAssociationRoleAndPermission(role.getId());
        roleDao.deleteAssociationRoleAndMenu(role.getId());
        //在中间表新增更改过后的关系
       setRoleAndMenu(role.getId(),menuIds);
       setRoleAndPermission(role.getId(),permissionIds);
        //更新CheckGroup表中的数据
        roleDao.edit(role);
    }

    @Override
    public List<Role> finAll() {
        return roleDao.findAll();
    }

    @Override
    public List<Integer> findRoleIdsByUserId(int id) {
        return roleDao.findRoleIdsByUserId(id);
    }


}
